/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package models;

import java.util.ArrayList;

/**
 *
 * @author Steffen
 */
public class Order {
    private int id;
    private Customer customer;
    private Employee employee;
    private int amount;
    private float totalPrice;
    private int deliveryStatus;
    private String deliveryDate = "";
    private String date = "";

    private ArrayList<OrderItem> orderItems = new ArrayList<OrderItem>();

    public Order (int id, Customer customer, Employee employee, int amount, float totalPrice, int deliveryStatus, String deliveryDate, String date)
    {
        this.id = id;
        this.customer = customer;
        this.employee = employee;
        this.amount = amount;
        this.totalPrice = totalPrice;
        this.deliveryStatus = deliveryStatus;
        this.deliveryDate = deliveryDate;
        this.date = date;
    }

    public Order (Customer customer, Employee employee, int amount, float totalPrice, int deliveryStatus, String deliveryDate, String date)
    {
        this.customer = customer;
        this.employee = employee;
        this.amount = amount;
        this.totalPrice = totalPrice;
        this.deliveryStatus = deliveryStatus;
        this.deliveryDate = deliveryDate;
        this.date = date;
    }

    public Order (int id, int amount, float totalPrice, int deliveryStatus, String deliveryDate, String date)
    {
        this.id = id;
        this.amount = amount;
        this.totalPrice = totalPrice;
        this.deliveryStatus = deliveryStatus;
        this.deliveryDate = deliveryDate;
        this.date = date;
    }


    public Order ()
    {

    }

    public void setOrderItems(ArrayList<OrderItem> oi) {
        this.orderItems = oi;
        updateTotalPrice();
    }

    public void addOrderItem(OrderItem oi) {
        orderItems.add(oi);
        updateTotalPrice();
    }

    public void removeOrderItem(OrderItem oi) {
        orderItems.remove(oi);
        this.updateTotalPrice();
    }

    public void removeOrderItem(int index) {
        orderItems.remove(index);
        this.updateTotalPrice();
    }

    public void removeOneQuantity(Product product) {
        if(product != null) {
            removeOneQuantity(product.getId());
        }

        this.updateTotalPrice();
    }

    public void removeOneQuantity(int id) {
        for(OrderItem oi : orderItems) {
            if(oi.getProduct().getId() == id) {

                oi.minusQuantity();

                if(oi.getQuantity() <= 0) {
                    orderItems.remove(oi);
                    break;
                }
            }
        }

        this.updateTotalPrice();
    }

    public boolean removeProduct(Product product) {
        boolean result = false;
        for(OrderItem oi : orderItems) {
            if(oi.getProduct().getId() == product.getId()) {
                result = orderItems.remove(oi);
                break;
            }
        }

        this.updateTotalPrice();
        return result;
    }

    public ArrayList<OrderItem> getOrderItems() {
        return orderItems;
    }

    public OrderItem getOrderItem(int index) {
        return orderItems.get(index);
    }

    public boolean addProduct(Product product, int quantity) {
        boolean result = false;
        OrderItem o = findProductItem(product);
        if(o != null) {
            o.plusQuantity();
            
            result = true;
        } else {
            OrderItem oi = new OrderItem(product, quantity);
            
            result = orderItems.add(oi);
        }
        
        updateTotalPrice();
        return result;
    }

    private OrderItem findProductItem(Product product) {
        OrderItem found = null;
        for(OrderItem oi : orderItems) {
            if(oi.getProduct().getId() == product.getId()) {
                found = oi;
                break;
            }
        }

        return found;
    }

    private void updateTotalPrice() {
        float total = 0;
        for(OrderItem oi : orderItems) {
            total += oi.getSubTotal();
        }

        this.setTotalPrice(total);
        this.updateAmount();
    }

    private void updateAmount() {
        int a = 0;
        for(OrderItem oi : orderItems) {
            a += oi.getQuantity();
        }

        this.amount = a;
     }

     /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the costumerid
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @param costumerid the costumerid to set
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    /**
     * @return the employee
     */
    public Employee getEmployee() {
        return employee;
    }

    /**
     * @param employee the employee to set
     */
    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    /**
     * @return the amount
     */
    public int getAmount() {
        return amount;
    }

    /**
     * @param amount the amount to set
     */
    public void setAmount(int amount) {
        this.amount = amount;
    }

    /**
     * @return the totalPrice
     */
    public float getTotalPrice() {
        return totalPrice;
    }

    /**
     * @param totalPrice the totalPrice to set
     */
    public void setTotalPrice(float totalPrice) {
        this.totalPrice = totalPrice;
    }

    /**
     * @return the deliveryStatus
     */
    public int getDeliveryStatus() {
        return deliveryStatus;
    }

    /**
     * @param deliveryStatus the deliveryStatus to set
     */
    public void setDeliveryStatus(int deliveryStatus) {
        this.deliveryStatus = deliveryStatus;
    }

    /**
     * @return the deliveryDate
     */
    public String getDeliveryDate() {
        return deliveryDate;
    }

    /**
     * @param deliveryDate the deliveryDate to set
     */
    public void setDeliveryDate(String deliveryDate) {
        this.deliveryDate = deliveryDate;
    }

    /**
     * @return the date
     */
    public String getDate() {
        return date;
    }

    /**
     * @param date the date to set
     */
    public void setDate(String date) {
        this.date = date;
    }
            
}
